package com.jinxin.phonecloudmusic.myapplication.fragment.fra;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.jinxin.phonecloudmusic.myapplication.R;
import com.jinxin.phonecloudmusic.myapplication.adapter.MusicSearchSongAdapter;
import com.jinxin.phonecloudmusic.myapplication.base.BaseFragment;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.Search;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.Song;
import com.jinxin.phonecloudmusic.myapplication.event.MusicControlEvent;
import com.jinxin.phonecloudmusic.myapplication.presenter.IPresenter.IMainPagePresenter;
import com.jinxin.phonecloudmusic.myapplication.presenter.PresenterImpl.IMainPagePresentImp;
import com.jinxin.phonecloudmusic.myapplication.utils.RxBus;
import com.jinxin.phonecloudmusic.myapplication.view.IMusicSearchView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by ZJ on 2017/10/26 0026.
 * 搜索碎片
 */

public class MusicSearchFragment extends BaseFragment implements IMusicSearchView {
    @BindView(R.id.et_search_text)
    EditText etSearchText;
    @BindView(R.id.img_search)
    ImageView imgSearch;
    @BindView(R.id.lv_music_search_result)
    ListView lvMusicSearchResult;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    /*@BindView(R.id.vp_music_search)
    ViewPager vpMusicSearch;*/

    private IMainPagePresenter iMainPagePresenter;
    private String text;
    private List<Song> singers=new ArrayList<>();

    private MusicSearchSongAdapter musicSearchSongAdapter;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    etSearchText.setText((String) msg.obj);
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    public View inflate(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fra_music_search, container, false);
    }

    @Override
    public void initData() {
        iMainPagePresenter=new IMainPagePresentImp(this);
        if (null!=getArguments().getString("searchText")){
            text= (String)getArguments().getString("searchText");
            handler.obtainMessage(1,text).sendToTarget();
        }
            if (null!=text)
                iMainPagePresenter.search("search",text);

        imgSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text=etSearchText.getText().toString();
                if (null!=text)
                    iMainPagePresenter.search("search",etSearchText.getText().toString());
            }
        });
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxBus.getDefault().post(new MusicControlEvent(1003,"",""));//关闭当前碎片
            }
        });
    }

    @Override
    public void getSongInfo(Search search) {
        singers=search.getSongList();
        //适配数据
        if (null==musicSearchSongAdapter){
            musicSearchSongAdapter=new MusicSearchSongAdapter(getActivity(),R.layout.item_music_search_song,singers);
        }else {
            musicSearchSongAdapter.replaceAll(singers);
        }
        if (null!=lvMusicSearchResult)
            lvMusicSearchResult.setAdapter(musicSearchSongAdapter);
        //点击进入播放界面
        lvMusicSearchResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RxBus.getDefault().post(new MusicControlEvent(30,singers,position,"musicSearch"));//跳转到播放界面(obj3:作为类别)
            }
        });
    }
}
